#include "Role.h"
#include <cmath>
#include "Skeleton.h"
#include <ctime>
#include <d3dx9.h>

const float Role::PI = 3.141592653;
Role::Role(void)
	:m_x(0), m_y(0), m_z(0), 
	m_rx(0), m_ry(0), m_rz(0),
	m_Model(0), m_XFileName(0)
{
}

Role::Role(LPCSTR xFileName)
	:m_x(0), m_y(0), m_z(0), 
	m_rx(0), m_ry(0), m_rz(0),
	m_Model(0), m_XFileName(xFileName)
{
}

Role::~Role(void)
{
}

void Role::moveForward(float d)
{
	m_z += d * sin( -m_ry + PI / 2);
	m_x += d * cos( -m_ry + PI / 2);
}

void Role::moveBack(float d)
{
	m_z -= d * sin( -m_ry + PI / 2);
	m_x -= d * cos( -m_ry + PI / 2);
}

void Role::moveLeft(float d)
{
	m_z -= d * sin( -m_ry);// + PI / 2 - PI / 2);
	m_x -= d * cos( -m_ry);// + PI / 2 - PI / 2);
}

void Role::moveRight(float d)
{
	m_z += d * sin( -m_ry);// + PI / 2 - PI / 2);
	m_x += d * cos( -m_ry);// + PI / 2 - PI / 2);
}

void Role::moveUp(float d)
{
	m_y = d;
}

void Role::moveDown(float d)
{
}


void Role::turnLeft(float a)
{
	m_ry -= a;
	//int s = floor(m_ry / PI);
	//m_ry -= a * PI;
}

void Role::turnRight(float a)
{
	m_ry += a;
	//int s = floor(m_ry / PI);
	//m_ry -= a * PI;
}

void Role::turnUp(float a)
{
}

void Role::turnDown(float a)
{
}

void Role::turnLeftZ(float a)
{
}

void Role::turnRightZ(float a)
{
}

void Role::setModel(Skeleton * pSkeleton)
{ 
	delete m_Model;
	m_Model = pSkeleton;
}

void Role::Rend()
{
	D3DXMATRIX mat;
	D3DXMATRIX mat1;
	D3DXMATRIX mat2;
	//D3DXMatrixIdentity(&mat);
	D3DXMatrixTranslation(&mat1, m_x, m_y, m_z);
	D3DXMatrixRotationY(&mat2, m_ry + PI);// + PI / 2);
	mat = mat2 * mat1;
	if(m_Model){
		if(m_State == Role::move)
		{
			long time = clock() / 40;
			m_Model->Update(time);
		}
		m_Model->UpdateHierarchy(&mat);
		m_Model->Rend();
	}
}